<?php
class LogsController extends  IndexController 
{
	public function init()
    {
		parent::init();
        $this->utl             = Yaf_Registry::get("utl");
        $this->Session         = Yaf_Registry::get("session");
        $this->ArticleModel    = new ArticleModel();
        $this->UserLogsModel   = new LogsModel();
        $this->UserModel       = new UserModel();
        $this->RobotsModel     = new RobotsModel();
        $this->SourceModel     = new SourceModel();
        $this->SitesModel      = new WebsiteModel();
        $this->TypicalModel    = new TypicalModel();
        $this->CategoryModel   = new CategoryModel();
    }
    public function userAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allUserLogs_info  = $this->UserLogsModel->getUserLogsInfo($startLimit,$offset);
        $allCounts         = $this->UserLogsModel->getUserAllCount();
        $allpage           = $this->utl::page('/Logs/user', $page, $allCounts[0]['rows'], $offset, $status='');
        $user_info         = $this->UserModel->getUserInfo();
        $userlogs_list     = array();
        foreach ($allUserLogs_info AS $k => $v) 
        {
            $userlogs_list[$k] = $v;
            
            $userlogs_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $userlogs_list[$k]['rows'] = $allCounts[0]['rows'];
            $userlogs_list[$k]['every_page'] = $offset;
            foreach ($user_info  AS $ks => $vs)
            {
                if ($v['user_id'] === $vs['user_id'])
                {
                    $userlogs_list[$k]['username'] = $vs['username'];break;
                }
            }
        }
        $this->getView()->assign("userlogs_info",$userlogs_list);
        $this->getView()->assign("allpage",$allpage);
    }

    public function userPageAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allUserLogs_info  = $this->UserLogsModel->getUserLogsInfo($startLimit,$offset);
        $allCounts         = $this->UserLogsModel->getUserAllCount();
        $allpage           = $this->utl::page('/Logs/user', $page, $allCounts[0]['rows'], $offset, $status='');
        $user_info         = $this->UserModel->getUserInfo();
        $userlogs_list     = array();
        foreach ($allUserLogs_info AS $k => $v) 
        {
            $userlogs_list[$k] = $v;
            $userlogs_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $userlogs_list[$k]['rows'] = $allCounts[0]['rows'];
            $userlogs_list[$k]['every_page'] = $offset;
            foreach ($user_info  AS $ks => $vs)
            {
                if ($v['user_id'] === $vs['user_id'])
                {
                    $userlogs_list[$k]['username'] = $vs['username'];break;
                }
            }
        }
        $this->getView()->assign("userlogs_info",$userlogs_list);
        exit(json_encode($userlogs_list, true));


    }

    public function userCurrentPageAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allUserLogs_info  = $this->UserLogsModel->getUserLogsInfo($startLimit,$offset);
        $allCounts         = $this->UserLogsModel->getUserAllCount();
        $allpage           = $this->utl::page('/Logs/user', $page, $allCounts[0]['rows'], $offset, $status='');
        exit($allpage);
    }
    
    public function pushAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $pushlogs_info     = $this->UserLogsModel->getPushLogsInfo($startLimit,$offset);
        $allCounts         = $this->UserLogsModel->getPushAllCount();
        $allpage           = $this->utl::page('/Logs/push', $page, $allCounts[0]['rows'], $offset, $status='');
        $user_info         = $this->UserModel->getUserInfo();
        //$article_info      = $this->ArticleModel->getArticleInfo();
        $sites_info        = $this->SitesModel->selectSites();
        $flourish_info     = $this->SitesModel->selectZhss();
        $category_info     = $this->CategoryModel->getCategoryInfo();
        $user_info         = $this->UserModel->getUserInfo();
        $userlogs_list     = array();
        foreach ($pushlogs_info AS $k => $v) 
        {
            $userlogs_list[$k] = $v;
            $userlogs_list[$k]['push_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $userlogs_list[$k]['rows'] = $allCounts[0]['rows'];
            $userlogs_list[$k]['every_page'] = $offset;
            foreach ($user_info  AS $ks => $vs) 
            {
                if ($v['user_id'] === $vs['user_id']) 
                {
                    $userlogs_list[$k]['username'] = $vs['username'];break;
                }
            }
            /*foreach ($article_info  AS $ks => $vs) 
            {
                if ($v['article_id'] === $vs['article_id']) 
                {
                    $userlogs_list[$k]['title'] = $vs['title'];break;
                }
            }*/
            foreach ($sites_info  AS $ks => $vs) 
            {
                if ($v['siteid'] === $vs['sid']) 
                {
                    $userlogs_list[$k]['sitename'] = $vs['name'];break;
                }
                
            }
            
            foreach ($category_info  AS $ks => $vs) 
            {
                if ($v['catid'] === $vs['catid'] && $v['siteid'] == '1') 
                {
                    $userlogs_list[$k]['catname'] = $vs['catname'];break;
                }
            }
            foreach ($flourish_info  AS $ks => $vs) 
            {
                if ($v['catid'] === $vs['catid'] && $v['siteid'] == '2') 
                {
                    $userlogs_list[$k]['catname'] = $vs['catname'];break;
                }
            }
           
        }
        foreach ($userlogs_list as $key => $value) 
        {
            if($value['status'] == 1)
            {
                $userlogs_list[$key]['article_status'] = '推送失败';
            }
            else if($value['status'] == 0){
                $userlogs_list[$key]['article_status'] ='未推送';
            }
            else if($value['status'] == 2){
                $userlogs_list[$key]['article_status'] ='已推送';
            }
        }
        $this->getView()->assign("pushlogs_info",$userlogs_list);
        $this->getView()->assign("allpage",$allpage);
        $this->getView()->assign("user_info",$user_info);
    }

    public function pushPageAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $pushlogs_info     = $this->UserLogsModel->getPushLogsInfo($startLimit,$offset);
        $allCounts         = $this->UserLogsModel->getPushAllCount();
        $allpage           = $this->utl::page('/Logs/push', $page, $allCounts[0]['rows'], $offset, $status='');
        $user_info         = $this->UserModel->getUserInfo();
        //$article_info      = $this->ArticleModel->getArticleInfo();
        $sites_info        = $this->SitesModel->selectSites();
        $flourish_info     = $this->SitesModel->selectZhss();
        $category_info     = $this->CategoryModel->getCategoryInfo();
        $userlogs_list     = array();
        foreach ($pushlogs_info AS $k => $v) 
        {
            $userlogs_list[$k] = $v;
            $userlogs_list[$k]['push_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $userlogs_list[$k]['rows'] = $allCounts[0]['rows'];
            $userlogs_list[$k]['every_page'] = $offset;
            foreach ($user_info  AS $ks => $vs) 
            {
                if ($v['user_id'] === $vs['user_id']) 
                {
                    $userlogs_list[$k]['username'] = $vs['username'];break;
                }
            }
            /*foreach ($article_info  AS $ks => $vs) 
            {
                if ($v['article_id'] === $vs['article_id']) 
                {
                    $userlogs_list[$k]['title'] = $vs['title'];break;
                }
            }*/
            foreach ($sites_info  AS $ks => $vs) 
            {
                if ($v['siteid'] === $vs['sid']) 
                {
                    $userlogs_list[$k]['sitename'] = $vs['name'];break;
                }  
            }
            
            foreach ($category_info  AS $ks => $vs) 
            {
                if ($v['catid'] === $vs['catid'] && $v['siteid'] == '1') 
                {
                    $userlogs_list[$k]['catname'] = $vs['catname'];break;
                }
            }
            foreach ($flourish_info  AS $ks => $vs) 
            {
                if ($v['catid'] === $vs['catid'] && $v['siteid'] == '2') 
                {
                    $userlogs_list[$k]['catname'] = $vs['catname'];break;
                }
            }
        }
       
        foreach ($userlogs_list as $key => $value) 
        {
            if($value['status'] == 1)
            {
                $userlogs_list[$key]['article_status'] = '推送失败';
            }
            else if($value['status'] == 0){
                $userlogs_list[$key]['article_status'] ='未推送';
            }
            else if($value['status'] == 2){
                $userlogs_list[$key]['article_status'] ='已推送';
            }
        }
        $this->getView()->assign("pushlogs_info",$userlogs_list);
        exit(json_encode($userlogs_list, true));
    }

    public function pushCurrentPageAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $pushlogs_info     = $this->UserLogsModel->getPushLogsInfo($startLimit,$offset);
        $allCounts         = $this->UserLogsModel->getPushAllCount();
        $allpage           = $this->utl::page('/Logs/push', $page, $allCounts[0]['rows'], $offset, $status='');
        exit($allpage);
    }

    public function collAction()
    {
        $offset             = 10;
        $page               = $this->getRequest()->getParam("page",1);
        $startLimit         = $offset*($page-1);
        $allUserLogs_info   = $this->UserLogsModel->getCollLogsInfo($startLimit,$offset);
        $allCounts          = $this->UserLogsModel->getCollAllCount();
        $allpage            = $this->utl::page('/Logs/coll', $page, $allCounts[0]['rows'], $offset, $status='');
        $typical_info       = $this->TypicalModel->getTypicalInfo();
        $userlogs_list      = array();
        $list               = array();
        $list['rows']       = $allCounts[0]['rows'];
        $list['every_page'] = $offset;
       // var_dump($allUserLogs_info);
        foreach ($allUserLogs_info AS $k => $v)
        {
            $userlogs_list[$k] = $v;
            if(!is_numeric($v['start_time']) && !is_numeric($v['end_time'])){
                $userlogs_list[$k]['coll_startTime']   = date('Y-m-d H:i:s',strtotime($v['start_time']));
                $userlogs_list[$k]['coll_endTime']     = date('Y-m-d H:i:s',strtotime($v['end_time']));
                $this->UserLogsModel->updateCollTime($userlogs_list[$k]['coll_startTime'],$userlogs_list[$k]['coll_endTime'],$v['Id']);
            }
            else{
                $userlogs_list[$k]['coll_startTime']   = date('Y-m-d H:i:s',(int) $v['start_time']);
                $userlogs_list[$k]['coll_endTime']     = date('Y-m-d H:i:s',(int) $v['end_time']);
            }
            
            if($v['level'] == "0"){
            $userlogs_list[$k]['level_status']     = "错误";
            }
            else if($v['level'] == "1"){
                $userlogs_list[$k]['level_status'] = "正确";
            }
			foreach ($typical_info  AS $ks => $vs)
            {
                if ($v['article_type_id'] === $vs['article_type_id'])
                {
                        $userlogs_list[$k]['typical_name'] = $vs['name'];break;
                }
            }
        }

        $this->getView()->assign("colllogs_info",$userlogs_list);
        $this->getView()->assign("list",$list);
        $this->getView()->assign("allpage",$allpage);
    }

    public function collPageAction()
    {
        $offset             = 10;
        $page               = $this->getRequest()->getParam("page",1);
        $startLimit         = $offset*($page-1);
        $allUserLogs_info   = $this->UserLogsModel->getCollLogsInfo($startLimit,$offset);
        $allCounts          = $this->UserLogsModel->getCollAllCount();
        $allpage            = $this->utl::page('/Logs/coll', $page, $allCounts[0]['rows'], $offset, $status='');
        $typical_info       = $this->TypicalModel->getTypicalInfo();
        $userlogs_list      = array();
        $list               = array();
        $list['rows']       = $allCounts[0]['rows'];
        $list['every_page'] = $offset;
        foreach ($allUserLogs_info AS $k => $v)
        {
            $userlogs_list[$k] = $v;
            if(!is_numeric($v['start_time']) && !is_numeric($v['end_time'])){
                $userlogs_list[$k]['coll_startTime']   = date('Y-m-d H:i:s',strtotime($v['start_time']));
                $userlogs_list[$k]['coll_endTime']     = date('Y-m-d H:i:s',strtotime($v['end_time']));
                $this->UserLogsModel->updateCollTime($userlogs_list[$k]['coll_startTime'],$userlogs_list[$k]['coll_endTime'],$v['Id']);
            }
            else{
                $userlogs_list[$k]['coll_startTime']   = date('Y-m-d H:i:s',(int) $v['start_time']);
                $userlogs_list[$k]['coll_endTime']     = date('Y-m-d H:i:s',(int) $v['end_time']);
            }
            if($v['level'] == "0"){
            $userlogs_list[$k]['level_status']     = "错误";
            }
            else if($v['level'] == "1"){
                $userlogs_list[$k]['level_status'] = "正确";
            }
            foreach ($typical_info  AS $ks => $vs)
            {
                if ($v['article_type_id'] === $vs['article_type_id'])
                {
                        $userlogs_list[$k]['typical_name'] = $vs['name'];break;
                }
            }
        }
        $this->getView()->assign("colllogs_info",$userlogs_list);
        exit(json_encode($userlogs_list, true));
    }

    public function collCurrentPageAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allUserLogs_info  = $this->UserLogsModel->getCollLogsInfo($startLimit,$offset);
        $allCounts         = $this->UserLogsModel->getCollAllCount();
        $allpage           = $this->utl::page('/Logs/coll', $page, $allCounts[0]['rows'], $offset, $status='');
        exit($allpage);
    }
  
    public function loginAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allUserLogs_info  = $this->UserLogsModel->selectLoginLogs($startLimit,$offset);
        $allCounts         = $this->UserLogsModel->getLoginAllCount();
        $allpage           = $this->utl::page('/Logs/login', $page, $allCounts[0]['rows'], $offset, $status='');
        $user_info         = $this->UserModel->getUserInfo();
        $userlogs_list     = array();
        foreach ($allUserLogs_info as $k => $v) {
            $userlogs_list[$k] = $v;
            $userlogs_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $userlogs_list[$k]['rows'] = $allCounts[0]['rows'];
            $userlogs_list[$k]['every_page'] = $offset;
            foreach ($user_info as $ks => $vs) {
                if ($v['user_id'] === $vs['user_id'])
                {
                        $userlogs_list[$k]['username'] = $vs['username'];break;
                }
            }
        }

        $this->getView()->assign("loginlogs_info",$userlogs_list);
        $this->getView()->assign("allpage",$allpage);
    }
    
    public function loginPageAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allUserLogs_info  = $this->UserLogsModel->selectLoginLogs($startLimit,$offset);
        $allCounts         = $this->UserLogsModel->getLoginAllCount();
        $allpage           = $this->utl::page('/Logs/login', $page, $allCounts[0]['rows'], $offset, $status='');
        $user_info     = $this->UserModel->getUserInfo();
        $userlogs_list     = array();
        foreach ($allUserLogs_info as $k => $v) {
            $userlogs_list[$k] = $v;
            $userlogs_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $userlogs_list[$k]['rows'] = $allCounts[0]['rows'];
            $userlogs_list[$k]['every_page'] = $offset;
            foreach ($user_info as $ks => $vs) {
                if ($v['user_id'] === $vs['user_id'])
                {
                        $userlogs_list[$k]['username'] = $vs['username'];break;
                }
            }
        }
        $this->getView()->assign("loginlogs_info",$userlogs_list);
        exit(json_encode($userlogs_list, true));
    }

    public function loginCurrentPageAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allUserLogs_info  = $this->UserLogsModel->selectLoginLogs($startLimit,$offset);
        $allCounts         = $this->UserLogsModel->getLoginAllCount();
        $allpage           = $this->utl::page('/Logs/login', $page, $allCounts[0]['rows'], $offset, $status='');
        exit($allpage);
    }

    public function pushInfoAction()
    {
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $push_info      = array();
            $user_info      = $this->UserModel->getUserInfo();
            $site_info      = $this->SitesModel->selectSites();
            $selectPushTime = $this->UserLogsModel->selectPushTime($post,$user_info);
            foreach ($selectPushTime as $key => $value) {
                $push_info[$key]['pushLastTime']  = date('Y-m-d H:i:s', (int) $value['time']);
                foreach ($user_info  AS $ks => $vs)
                {
                    if ($value['user_id'] == $vs['user_id'])
                    {
                        $push_info[$key]['user_name'] = $vs['realname'];break;
                    }
                }
                foreach ($site_info  AS $ks => $vs)
                {
                    if ($value['siteid'] == $vs['sid'])
                    {
                        $push_info[$key]['site_name'] = $vs['name'];break;
                    }
                }
            }
            $result  =  json_encode($push_info);
            exit($result);
        }
    }
    
    
}
?>